package controllers;

import java.util.Calendar;
import java.util.List;

import play.mvc.With;

import models.Information;
import models.Managers;
import models.Programa;
import service.InformationService;
import service.ProgramaService;

@Check("InformationManage")
@With(Secure.class)
@CRUD.For(Information.class)
public class InformationAction extends CRUD{
	
	/**
	 * 查看全部信息的方法
	 */
	public static void list(){
		List<Information> informations = InformationService.findAll();
		render(informations);
	}
	
	/**
	 * 跳转向添加页面
	 */
	public static void blank(){
		List<Programa> programas = ProgramaService.findTop();
		render(programas);
	}
	
	
	/**
	 * 添加信息的方法
	 * 
	 * @param information  信息对象
	 * @param proId        所属栏目id
	 */
	public static void create(Information information,Long[] proId){
		Long id=0l;
		for (Long long1 : proId) {
			if(long1!=0l){
				id = long1;
			}
		}
		information.managers = Managers.findById(1l);
		information.programa = ProgramaService.findById(id);
		information.infTime = Calendar.getInstance().getTime();
		try {
			InformationService.add(information);
			flash.success("添加成功");
			list();
		} catch (Exception e) {
			System.out.println("类Information中的create（）方法抛异常");
			e.printStackTrace();
			flash.success("添加失败");
			blank();
		}
	}
	
	/**
	 * 跳转向修改页面的方法
	 * 
	 * @param id   信息id
	 */
	public static void show(Long id){
		Information information = InformationService.findById(id);
		List<Programa> programas = ProgramaService.findTop();
		render(information,programas);
	}
	
	/**
	 * 信息的修改方法
	 * 
	 * @param information 信息对象
	 * @param proId       栏目id
	 */
	public static void save(Information information,Long[] proId){
		Long id=0l;
		for (Long long1 : proId) {
			if(long1!=0l){
				id = long1;
			}
		}
		information.managers = Managers.findById(1l);
		information.programa = ProgramaService.findById(id);
		information.infTime = Calendar.getInstance().getTime();
		try {
			InformationService.update(information);
			flash.success("修改成功");
			list();
		} catch (Exception e) {
			System.out.println("####################InformationAction.save() Exception#######################");
			e.printStackTrace();
			flash.success("修改失败");
			blank();
		}
	}
	
	/**
	 * 删除信息的方法
	 * 
	 * @param id 信息id
	 */
	public static void delete(Long id){
		try {
			InformationService.delete(id);
			flash.success("删除成功");
			list();
		} catch (Exception e) {
			System.out.println("#########################InformationAction.delete() Exception##############################");
			e.printStackTrace();
			flash.success("删除失败");
			list();
		}

	}
}
